<?php
	/*
	 * File: update_inc.php
	 * Last modification: 21.07.2011
	 * Author(s): kivio.wanderley
	 * 
	 */
?>
	
<?php /** UPPER PHP CODE */
	# Requires
	$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR;
	require_once($t_core_dir . 'core_api.php');

	# Try to make the connection
	if( !datab_is_connected() ) {
		config_get_global_array();
		datab_connect( config_get('host'), (int)config_get('port'), config_get('database'), 
					   config_get('user'), config_get('password') );
		config_unset();						   
	}
	
	# Start session
	if( ! isset($_SESSION) ) session_start();
	
	# Prepare variables
	require('update_inc_vars.php');

	$t_type = gpc_get_string('update_type');
	$t_type_id = gpc_get_int('type_id');
	$t_type_user_id = gpc_get_int('type_user_id');

?>
	
<?php  /** CENTER PHP CODE */
	if( $t_type == 'people' ) { # For people 
		$people_option_list = $t_type_id;
		
		/* Update people */
		$i = 0;
		$t_query = "UPDATE tb_usuario 
					SET ";
					if($upd_cpf != '' && $upd_cpf != "NULL") 									{ $i++; $t_query .= "cpf = $upd_cpf,"; } 
					if($upd_first_name != '' && $upd_first_name != "NULL") 						{ $i++; $t_query .= "nome = $upd_first_name,"; }
					if($upd_last_name != '' && $upd_last_name != "NULL") 						{ $i++; $t_query .= "sobrenome = $upd_last_name,"; } 
		$t_query .= "WHERE id = $people_option_list;";
		$t_query = str_replace(',WHERE',' WHERE', $t_query); //Erase last comma
		if( $i > 0) {
			$t_row = datab_query($t_query);
			# If result got en error
			if(is_string($t_row)) { set_info_msg($t_row, I_ERR); }
		} 
		
		unset($i);
	}
	if ( $t_type == 'user' ) { # For user 
		$user_option_list = $t_type_id;
		$user_id = $t_type_user_id;
		
		/* Update user */
		$i = 0;
		$t_query = "UPDATE tb_login 
					SET ";
					if($upd_user != '' && $upd_user != "NULL") 					{ $i++; $t_query .= "user = $upd_user,"; }
					if($upd_access_list != '' && $upd_access_list != "NULL") 	{ $i++; $t_query .= "nivel = $upd_access_list,"; }
		$t_query .= "WHERE usuario_id = $t_type_id;";
		$t_query = str_replace(',WHERE',' WHERE', $t_query); //Erase last comma
		if( $i > 0) {
			$t_row = datab_query($t_query);
			if(is_string($t_row)) { set_info_msg($t_row, I_ERR); }
		} 		

		unset($i);
	}
	if ( $t_type != 'people' && $t_type != 'user' ) {
		set_info_msg("NO TYPE FOR UPDATING", I_ERR);
	}
	
	//print_header_redirect( config_get('uri_prefix')."main_page.php?page=update_page&type_option_list=$t_type&type_id=$t_type_id" );
	set_info_msg("UPDATE SAVE SUCCESS", I_OK, config_get('uri_prefix')."main_page.php?page=update_page&type_option_list=".$t_type."&type_id=".$t_type_id);
?>